﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using ZLCMS.Business;
using System.Collections;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using ZLCMS.Business.DwzFrame;
using System.Reflection;
using NHibernate;
using ZLCMS.Data.Common;
using ZLCMS.Business.UploadFileInfo;
using Newtonsoft.Json;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.SystemManage.Basics.SearchProperty
{
    public partial class SearchPropertyEdit : System.Web.UI.Page
    {
        protected FrameHelper _framePage;
        protected ShopSearchPropertyGroup _categorySearchProperty;
        protected ShopCategory _category;
        public string _userAccount;
       protected  int id;
        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = UserKit.GetUserAccountFailLogin();
            if (!string.IsNullOrWhiteSpace(Request["cid"]))
                doc_cid.Value = Request["cid"];
            //初始化DWZ控件
            _framePage = new FrameHelper(this);

            //初始化全局变量值
            InitializeGlobalValue();

            //初始化控件值
            InitializeContent();

            if (IsPostBack)
            {
                Save();
            }
        }
        protected void InitializeGlobalValue()
        {
            
            try
            {
                if (!IsPostBack)
                {
                    if (string.IsNullOrWhiteSpace(Request["uid"]))
                    {
                        ViewState["uid"] = null;
                        id = 0;
                        //return;
                    }
                    else
                    { 
                        id = int.Parse( Request["uid"]);
                        ViewState["uid"] = id;
                    }
                }
                else
                {
                    if (ViewState["uid"] == null)
                    {
                        return;
                    }
                    id = (int)ViewState["uid"];
                }

                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    _categorySearchProperty = session.CreateCriteria<ShopSearchPropertyGroup>().Add(Expression.Eq("Id",id)).UniqueResult<ShopSearchPropertyGroup>();
                    _category = session.CreateCriteria<ShopCategory>().Add(Expression.Eq("Id", Convert.ToInt32( doc_cid.Value))).UniqueResult<ShopCategory>();
                }

                if (_categorySearchProperty == null&&id!=0)
                {
                    throw new Exception("找不到对应信息.");
                }
            }
            catch (System.Exception ex)
            {
                _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                return;
            }
        }

        protected void InitializeContent()
        {
            if (!IsPostBack)
            {
                if (_categorySearchProperty != null)
                {
                    if(_categorySearchProperty.ParentId>0){
                        ddl_property.SelectedValue = _categorySearchProperty.ParentId.ToString();
                    }
                    txt_property_value.Text = _categorySearchProperty.PropertyValue;
                    txt_sort.Text = _categorySearchProperty.Sort.ToString();
                    BindSearchPropertyItem(_categorySearchProperty.Id);
                    ckb_has_children.Checked = _categorySearchProperty.HasChildren == "1";
                }
                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    IList<ShopSearchPropertyGroup> groupList = session.CreateQuery("from ShopSearchPropertyGroup where CategoryId=:cid and HasChildren='1' and Id!=:id ").SetParameter("id", id).SetParameter("cid", Convert.ToInt32(doc_cid.Value)).List<ShopSearchPropertyGroup>();
                    groupList.Insert(0, new ShopSearchPropertyGroup() { Id = -1, PropertyValue = "--请选择--" });
                    ddl_property.DataSource = groupList;
                    ddl_property.DataTextField = "PropertyValue";
                    ddl_property.DataValueField = "Id";
                    ddl_property.DataBind();
                }
            }
        }
        protected void Save()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                try
                {
                if (_categorySearchProperty == null)
                {
                    _categorySearchProperty = new ShopSearchPropertyGroup();

                        if (!string.IsNullOrWhiteSpace(doc_cid.Value))
                        {
                            _categorySearchProperty.CategoryId = int.Parse(doc_cid.Value);
                        }
                    }
                    _categorySearchProperty.HasChildren = ckb_has_children.Checked == true ? "1" : "0";
                    _categorySearchProperty.PropertyValue = txt_property_value.Text;
                    _categorySearchProperty.Sort = Convert.ToInt32(string.IsNullOrEmpty(txt_sort.Text) ? "9999" : txt_sort.Text);
                    _categorySearchProperty.ParentId = int.Parse(ddl_property.SelectedValue!="-1"?ddl_property.SelectedValue:"0");

                session.SaveOrUpdate(_categorySearchProperty);
                session.Flush();   
            }
                catch (System.Exception ex)
                {
                    _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                    return;
                }
            }

            _framePage.Alert(new ReturnCode()
            {
                statusCode = 200,
                message = "操作成功!",
                callbackType = "forward",
                forwardUrl = "/SystemManage/Basics/SearchProperty/SearchPropertyEdit.aspx?tabid=foodedit&cid=" + doc_cid.Value + "&uid=" + _categorySearchProperty.Id
            });
        }

        protected void BindSearchPropertyItem(int propertyId)
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                rpt_property.DataSource = session.CreateCriteria<ShopSearchPropertyItem>().Add(Expression.Eq("SearchPropertyId", propertyId)).List<ShopSearchPropertyItem>();
                rpt_property.DataBind();
            }
        }
    }
}
